package com.ms.controller.user;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ms.constant.GenreTypeConstant;
import com.ms.constant.MessageConstant;
import com.ms.domain.dto.GenreUpdateDTO;
import com.ms.domain.entity.Genre;
import com.ms.domain.vo.GenreSearchVO;
import com.ms.result.Result;
import com.ms.service.GenreService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/genre")
@Slf4j
@Tag(name = "C端曲风相关接口")
@RequiredArgsConstructor
public class GenreController {

    private final GenreService genreService;

    /**
     * 获取曲风信息
     * @param id 曲风id
     * @return 曲风信息
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据 id 获取曲风信息")
    public Result<Genre> getGenre(@PathVariable Integer id) {
        return Result.success(genreService.getGenre(id));
    }

    /**
     * 获取一级曲风列表
     *
     * @return 一级曲风列表
     */
    @GetMapping("/main")
    @Operation(summary = "获取一级曲风接口")
    public Result<List<Genre>> getMainGenres() {
        return Result.success(genreService.getGenresByType(GenreTypeConstant.MAIN));
    }

    /**
     * 根据一级曲风获取二级曲风列表
     *
     * @param parentId 一级曲风ID
     * @return 二级曲风列表
     */
    @GetMapping("/sub")
    @Operation(summary = "根据一级曲风获取二级曲风接口")
    public Result<List<Genre>> getSubGenres(@RequestParam Integer parentId) {
        return Result.success(genreService.getGenresByParent(parentId, GenreTypeConstant.SUB));
    }

    /**
     * 根据二级曲风获取三级曲风列表
     *
     * @param parentId 二级曲风ID
     * @return 三级曲风列表
     */
    @GetMapping("/subtype")
    @Operation(summary = "根据二级曲风获取三级曲风接口")
    public Result<List<Genre>> getSubtypeGenres(@RequestParam Integer parentId) {
        return Result.success(genreService.getGenresByParent(parentId, GenreTypeConstant.SUBTYPE));
    }

    /**
     * 获取曲风树
     *
     * @return 曲风树
     */
    @GetMapping("/tree")
    @Operation(summary = "获取曲风树接口")
    public Result<List<Map<String, Object>>> getGenreTree() {
        return Result.success(genreService.getGenreTree());
    }

    /**
     * 添加曲风
     *
     * @param genre 曲风信息
     * @return 添加结果
     */
    @PostMapping
    @Operation(summary = "添加曲风接口")
    public Result<Void> addGenre(@RequestBody Genre genre) {
        log.info("添加曲风，信息：{}", genre);
        try {
            genreService.createGenre(genre);
            return Result.success();
        } catch (Exception e) {
            return Result.badRequest(e.getMessage());
        }
    }

    /**
     * 修改曲风
     *
     * @param id  曲风id
     * @param dto 修改信息
     * @return 修改结果
     */
    @PutMapping("/{id}")
    @Operation(summary = "修改曲风接口")
    public Result<Void> updateGenre(
            @PathVariable Integer id,
            @RequestBody @Valid GenreUpdateDTO dto) {
        log.info("修改曲风，id：{}，修改信息：{}", id, dto);
        try {
            genreService.updateGenre(id, dto);
            return Result.success();
        } catch (Exception e) {
            return Result.badRequest(e.getMessage());
        }
    }

    /**
     * 删除曲风
     *
     * @param id 曲风id
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除曲风接口")
    public Result<Void> deleteGenre(@PathVariable Integer id) {
        log.info("删除曲风，id：{}", id);
        try {
            genreService.deleteGenre(id);
            return Result.success();
        } catch (Exception e) {
            return Result.notFound(e.getMessage());
        }
    }

    /**
     * 搜索曲风
     *
     * @param keyword 关键字
     * @param type    类型
     * @return 搜索结果
     */
    @GetMapping("/search")
    @Operation(summary = "搜索曲风接口")
    public Result<List<GenreSearchVO>> searchGenres(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String type) {
        log.info("搜索曲风，关键字：{}，类型：{}", keyword, type);
        return Result.success(genreService.searchGenres(keyword, type));
    }

    /**
     * 分页搜索曲风
     *
     * @param keyword 关键字
     * @param type    类型
     * @param page    页码
     * @param size    每页数量
     * @return 搜索结果
     */
    @GetMapping("/search/page")
    @Operation(summary = "分页搜索曲风接口")
    public Result<Page<GenreSearchVO>> searchGenresPage(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String type,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        log.info("分页搜索曲风，关键字：{}，类型：{}，页码：{}，大小：{}", keyword, type, page, size);
        return Result.success(genreService.searchGenresPage(keyword, type, page, size));
    }

    /**
     * 根据音乐id查询对应的三级曲风
     *
     * @param musicId
     * @return
     */
    @Operation(summary = "根据音乐id查询对应的三级曲风")
    @GetMapping("/genres/{musicId}")
    public Result<List<Genre>> getGenreByMusicId(@PathVariable Integer musicId) {
        return Result.success(genreService.getGenreByMusicId(musicId));
    }

}